/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.account.core.service.query;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import info.peigen.hotpot.business.account.core.domain.account.action.AccountConvertor;
import info.peigen.hotpot.business.account.core.repository.entity.AccountEntity;
import info.peigen.hotpot.business.account.facade.QueryAccountServiceFacade;
import info.peigen.hotpot.business.account.facade.info.AccountInfo;
import info.peigen.hotpot.business.account.facade.order.query.QueryAccountByCustomerIdOrder;
import info.peigen.hotpot.business.account.facade.order.query.QueryCustomerAccountPageOrder;
import info.peigen.hotpot.business.account.facade.order.query.QueryFeeAccountOrder;
import info.peigen.hotpot.business.account.facade.result.query.QueryAccountResult;
import info.peigen.hotpot.business.account.facade.result.query.QueryFeeAccountResult;
import info.peigen.hotpot.business.account.facade.result.query.QueryPageAccountResult;
import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.ServiceQuery;
import info.peigen.hotpot.common.service.base.ServiceQueryContext;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.util.BeetlSqlPageResultUtil;
import org.beetl.sql.core.query.Query;
import org.noear.solon.annotation.Component;

import java.util.List;

/**
 * <b>(QueryAccountService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/8/31
 */
@SuppressWarnings("ALL")
@Component
public class QueryAccountService extends AbstractAccountQueryService implements QueryAccountServiceFacade {

    @Override
    public QueryAccountResult queryAccountByCustomerId(QueryAccountByCustomerIdOrder order) {
        ServiceQueryContext serviceContext = new ServiceQueryContext().setOrder(order).setResult(new QueryAccountResult());

        query(serviceContext, new ServiceQuery() {
            @Override
            public void query(ServiceQueryContext serviceContext) {
                // 1. 查 Entity
                AccountEntity entity = accountRepository.getMapper().findByCustomerId(order.getCustomerId());
                // 2. 一系列校验,保障转换步骤可以完成
                Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "查询[账户信息]失败![customerId=" + order.getCustomerId() + "]"));
                serviceContext.setEntity(entity);
            }

            @Override
            public void convert(ServiceQueryContext serviceContext) {
                // 3. 转换 Info
                AccountEntity entity      = (AccountEntity) serviceContext.getEntity();
                AccountInfo   accountInfo = AccountConvertor.convertAccountInfo(entity);
                ((QueryAccountResult) serviceContext.getResult()).fromSuccess(accountInfo);
            }
        });
        return (QueryAccountResult) serviceContext.getResult();
    }

    @Override
    public QueryPageAccountResult queryCustomerAccountPage(QueryCustomerAccountPageOrder order) {
        ServiceQueryContext serviceContext = new ServiceQueryContext().setOrder(order).setResult(new QueryPageAccountResult());

        query(serviceContext, new ServiceQuery() {
            @Override
            public void query(ServiceQueryContext serviceContext) {
                // 1. 查 Entity
                org.beetl.sql.core.page.PageResult<AccountEntity> accountEntityPageResult = accountRepository.getMapper().createLambdaQuery()
                        .andEq(AccountEntity::getCustomerId, Query.filterEmpty(order.getCustomerId()))
                        .andEq(AccountEntity::getAccountId, Query.filterEmpty(order.getAccountId()))
                        .andEq(AccountEntity::getCurrency, Query.filterNull(order.getCurrency()))
                        .andEq(AccountEntity::getAccountType, Query.filterNull(order.getAccountStatus()))
                        .andEq(AccountEntity::getStatus, Query.filterNull(order.getAccountType()))
                        .desc(AccountEntity::getCreateTime)
                        .page(order.getPageNum(), order.getPageSize());
                List<AccountEntity> accountEntities = accountEntityPageResult.getList();
                Assert.notNull(accountEntities, () -> new BusinessException(ResultCode.FAILURE, "分页查询[账户信息]失败![order=" + order + "]"));
                Assert.isTrue(CollUtil.isNotEmpty(accountEntities), () -> new BusinessException(ResultCode.FAILURE, "分页查询[账户信息]失败![order=" + order + "]"));
                serviceContext.setPage(BeetlSqlPageResultUtil.beetlSqlPageResultFromSuccess(accountEntityPageResult));
            }

            @Override
            public void convert(ServiceQueryContext serviceContext) {
                // 3. 转换 Info
                PageResult<AccountEntity> accountEntityPageResult = serviceContext.getPage();
                PageResult<AccountInfo>   accountInfoPageResult   = accountEntityPageResult.convert(entity -> AccountConvertor.convertAccountInfo(entity));
                ((QueryPageAccountResult) serviceContext.getResult()).fromSuccess(accountInfoPageResult.getInfo());
            }
        });
        return (QueryPageAccountResult) serviceContext.getResult();
    }

    @Override
    public QueryFeeAccountResult queryFeeAccount(QueryFeeAccountOrder order) {
        ServiceQueryContext serviceContext = new ServiceQueryContext().setOrder(order).setResult(new QueryFeeAccountResult());

        query(serviceContext, new ServiceQuery() {
            @Override
            public void query(ServiceQueryContext serviceContext) {
                // 1. 查 Entity
                AccountEntity accountEntity = null;
            }

            @Override
            public void convert(ServiceQueryContext serviceContext) {
                // 2. 转换 Info
                AccountEntity entity = (AccountEntity) serviceContext.getEntity();
                if (ObjectUtil.isNotNull(entity)) {
                    AccountInfo accountInfo = AccountConvertor.convertAccountInfo(AccountConvertor.convertAccount(entity));
                    ((QueryFeeAccountResult) serviceContext.getResult()).fromSuccess(accountInfo);
                }
            }
        });
        return (QueryFeeAccountResult) serviceContext.getResult();
    }

}